<template>
  <div>
    <!-- 封面图 -->
    <div class="banner" :style="articleCover">
      <div class="article-info-container">
        <!-- 文章标题 -->
        <div class="article-title">{{ article.title }}</div>
        <div class="article-info">
          <div class="first-line">
            <!-- 发表时间 -->
            <span>
              <i class="iconfont iconrili" />
              发表于 {{ article.createdAt | date }}
            </span>
            <span class="separator">|</span>
            <!-- 发表时间 -->
            <span>
              <i class="iconfont icongengxinshijian" />
              更新于
              <template v-if="article.updatedAt">
                {{ article.updatedAt | date }}
              </template>
              <template v-else>
                {{ article.createdAt | date }}
              </template>
            </span>
            <span class="separator">|</span>
            <!-- 文章分类 -->
            <span class="article-category">
              <i class="iconfont iconfenlei1" />
              <router-link :to="`/categories/${cate._id}`">
                {{ cate.name }}
              </router-link>
            </span>
            <!-- 文章标签 -->
            <span class="article-category" v-for="it in tag" :key="it._id">
              <i class="iconfont iconbiaoqian" />
              <router-link :to="`/tags/${it._id}`">
                {{ it.name }}
              </router-link>
            </span>
          </div>
          <div class="second-line">
            <!-- 字数统计 -->
            <span>
              <i class="iconfont iconzishu" />
              字数统计: {{ wordNum | num }}
            </span>
            <span class="separator">|</span>
            <!-- 阅读时长 -->
            <span>
              <i class="iconfont iconshijian" />
              阅读时长: {{ readTime }}
            </span>
          </div>
        </div>
      </div>
    </div>
    <!-- 内容 -->
 
      <v-row>
    <v-col md="9" cols="12">
      <v-card class="blog-container">
        <article
          class="article-content markdown-body"
          v-html="article.body"
          ref="article"
        />
      </v-card>
    </v-col>
    <v-col md="3" cols="12"  class="d-md-block d-none">
      <div class="toc-wrapper">
        <v-card class="blog-card animated zoomIn mt-5 big">
          <div class="web-info-title">
            目录
          </div>
          <div style="font-size: 0.875rem"
          class="tocL"
          v-for="item in articleToc"
          :key="item.id"
           @click="scrollTo(item.id)"
           :title="item.text">{{item.text}}</div>
        </v-card>
      </div>
    </v-col>
    </v-row>

    
  </div>
</template>

<script>
import Clipboard from "clipboard";
import MarkdownIt from "markdown-it";
import hljs from "highlight.js";
import "highlight.js/styles/tomorrow-night-eighties.css";
import Toc from "../../plugins/Toc.js";
export default {
  props: {
    id: { required: true },
  },
  created() {
    this.getArticle();
  },
  destroyed() {
    this.clipboard.destroy();
  },
  data: function () {
    return {
      imgList: [],
      article: {},
      count: 0,
      wordNum: "",
      readTime: "",
      articleHref: window.location.href,
      articleToc: [],
      cate: {},
      tag: {},
    };
  },
  methods: {
    async getArticle() {
      const that = this;
      const res = await this.$http.get(`articles/list/${this.id}`);
      this.markdownToHtml(res.data);
      this.$nextTick(() => {
        //统计文章字数
        this.wordNum = this.deleteHTMLTag(this.article.body).length;
        //计算阅读时间
        this.readTime = Math.round(this.wordNum / 400) + "分钟";
        //添加代码复制功能
        this.clipboard = new Clipboard(".copy-btn");
        this.clipboard.on("success", () => {
          this.$toast({ type: "success", message: "复制成功" });
        });
        //添加图片预览功能
        const imgList = this.$refs.article.getElementsByTagName("img");
        for (var i = 0; i < imgList.length; i++) {
          this.imgList.push(imgList[i].src);
          imgList[i].style.cssText = "cursor:zoom-in;";
          imgList[i].addEventListener("click", function (e) {
            that.previewImg(e.toElement.src);
          });
        }
      });
    },
    markdownToHtml(article) {
      const md = new MarkdownIt({
        html: true,
        linkify: true,
        typographer: true,
        highlight: function (str, lang) {
          // 当前时间加随机数生成唯一的id标识
          const codeIndex = parseInt(Date.now());
          // 复制功能主要使用的是 clipboard.js
          let html = `<button class="copy-btn iconfont iconfuzhi" type="button" data-clipboard-action="copy" data-clipboard-target="#copy${codeIndex}"></button>`;
          const linesLength = str.split(/\n/).length - 1;
          // 生成行号
          let linesNum = '<span aria-hidden="true" class="line-numbers-rows">';
          for (let index = 0; index < linesLength; index++) {
            linesNum = linesNum + "<span></span>";
          }
          linesNum += "</span>";
          if (lang && hljs.getLanguage(lang)) {
            // highlight.js 高亮代码
            const preCode = hljs.highlight(lang, str, true).value;
            html = html + preCode;
            if (linesLength) {
              html += '<b class="name">' + lang + "</b>";
            }
            // 将代码包裹在 textarea 中，由于防止textarea渲染出现问题，这里将 "<" 用 "<" 代替，不影响复制功能
            return `<pre class="hljs"><code>${html}</code>${linesNum}</pre><textarea style="position: absolute;top: -9999px;left: -9999px;z-index: -9999;" id="copy${codeIndex}">${str.replace(
              /<\/textarea>/g,
              "</textarea>"
            )}</textarea>`;
          }
        },
      });
      //将markdown替换为html标签
      article.body = md.render(article.body);
      this.article = article;
      this.cate.name = article.categories[0].name;
      this.cate._id = article.categories[0]._id;
      let tagArr=[];    
      article.tags.forEach(v => {
        let obj ={}
        obj.name = v.name
        obj._id = v._id
        tagArr.push(obj)
      });
      this.tag = tagArr
      //目录
      let tocData = Toc(md.render(article.body));
      this.article.body = tocData.article;
      this.articleToc = tocData.toc;
    },
    previewImg(img) {
      this.$imagePreview({
        images: this.imgList,
        index: this.imgList.indexOf(img),
      });
    },
    deleteHTMLTag(content) {
      return content
        .replace(/<\/?[^>]*>/g, "")
        .replace(/[|]*\n/, "")
        .replace(/&npsp;/gi, "");
    },
    scrollTo(id) {
      // 绑定 toc 点击事件
      let node = document.querySelector('[data-id="' + id + '"]');
      if (!node) {
        return;
      }
      node.scrollIntoView({
        behavior: "smooth",
        block: "center",
        inline: "nearest"
      });
    },
  },
  computed: {
    articleCover() {
      return (
        "background: url(" +
        this.article.icon +
        ") center center / cover no-repeat"
      );
    },
  },
};
</script>

<style scoped>
.banner:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}
.toc-wrapper {
  position: -webkit-sticky !important;
  position: sticky !important;
  top: 80px;
  margin-top: 380px;
  margin-right: 100px;
}
.tocL:hover{
  color: red;
}
.blog-card {
  line-height: 2;
  padding: 1.25rem 1.5rem;
}
.article-info i {
  font-size: 14px;
}
.article-info {
  font-size: 14px;
  line-height: 1.75;
  display: inline-block;
}
.article-container:hover {
  box-shadow: 0 4px 12px 12px rgba(7, 17, 27, 0.15);
}
@media (min-width: 760px) {
  .banner {
    color: #eee !important;
  }
  .article-info span {
    font-size: 95%;
  }
  .article-info-container {
    position: absolute;
    bottom: 1.9rem;
    padding: 0 8%;
    width: 100%;
  }
  .second-line,
  .third-line {
    display: inline;
  }
  .article-title {
    font-size: 1.875rem;
    margin-bottom: 0.4rem;
  }
}
@media (max-width: 759px) {
  .banner {
    color: #eee !important;
    height: 360px;
  }
  .article-info span {
    font-size: 90%;
  }
  .separator:first-child {
    display: none;
  }
  .blog-container {
    margin: 322px 5px 0 5px;
  }
  .article-info-container {
    position: absolute;
    bottom: 1.3rem;
    padding: 0 5%;
    width: 100%;
    color: #eee;
    text-align: left;
  }
  .article-title {
    font-size: 1.5rem;
    margin-bottom: 0.4rem;
  }
}
.article-content {
  word-break: break-word;
  font-size: 1rem;
  line-height: 1.8;
}
.article-operation {
  display: flex;
  align-items: center;
}
.article-category a {
  color: #fff !important;
}
.tag-container a {
  display: inline-block;
  margin: 0.5rem 0.5rem 0.5rem 0;
  padding: 0 0.75rem;
  width: fit-content;
  border: 1px solid #49b1f5;
  border-radius: 1rem;
  color: #49b1f5 !important;
  font-size: 12px;
  line-height: 2;
}
.tag-container a:hover {
  color: #fff !important;
  background: #49b1f5;
  transition: all 0.5s;
}
.aritcle-copyright {
  position: relative;
  margin-top: 40px;
  margin-bottom: 10px;
  font-size: 0.875rem;
  line-height: 2;
  padding: 0.625rem 1rem;
  border: 1px solid #eee;
}
.aritcle-copyright span {
  color: #49b1f5;
  font-weight: bold;
}
.aritcle-copyright a {
  text-decoration: underline !important;
  color: #99a9bf !important;
}
.aritcle-copyright:before {
  position: absolute;
  top: 0.7rem;
  right: 0.7rem;
  width: 1rem;
  height: 1rem;
  border-radius: 1rem;
  background: #49b1f5;
  content: "";
}
.aritcle-copyright:after {
  position: absolute;
  top: 0.95rem;
  right: 0.95rem;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 0.5em;
  background: #fff;
  content: "";
}
.article-reward {
  margin: 5rem 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
.reward-btn {
  position: relative;
  display: inline-block;
  width: 100px;
  background: #49b1f5;
  margin: 0 1rem;
  color: #fff !important;
  text-align: center;
  line-height: 36px;
  font-size: 0.875rem;
}
.reward-btn:hover .reward-main {
  display: block;
}
.reward-main {
  display: none;
  position: absolute;
  bottom: 40px;
  left: 0;
  margin: 0;
  padding: 0 0 15px;
  width: 100%;
}
.reward-all {
  display: inline-block;
  margin: 0 0 0 -110px;
  padding: 20px 10px 8px !important;
  width: 320px;
  border-radius: 4px;
  background: #f5f5f5;
}
.reward-all:before {
  position: absolute;
  bottom: -10px;
  left: 0;
  width: 100%;
  height: 20px;
  content: "";
}
.reward-all:after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 2px;
  left: 0;
  margin: 0 auto;
  width: 0;
  height: 0;
  border-top: 13px solid #f5f5f5;
  border-right: 13px solid transparent;
  border-left: 13px solid transparent;
}
.reward-item {
  display: inline-block;
  padding: 0 8px;
  list-style-type: none;
}
.reward-img {
  width: 130px;
  height: 130px;
  display: block;
}
.reward-desc {
  margin: -5px 0;
  color: #858585;
  text-align: center;
}
.like-btn {
  display: inline-block;
  width: 100px;
  background: #969696;
  color: #fff !important;
  text-align: center;
  line-height: 36px;
  font-size: 0.875rem;
}
.like-btn-active {
  display: inline-block;
  width: 100px;
  background: #ec7259;
  color: #fff !important;
  text-align: center;
  line-height: 36px;
  font-size: 0.875rem;
}
</style>

<style lang="scss">
pre.hljs {
  padding: 12px 2px 12px 40px !important;
  border-radius: 5px !important;
  position: relative;
  font-size: 14px !important;
  line-height: 22px !important;
  overflow: hidden !important;
  &:hover .copy-btn {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  code {
    display: block !important;
    margin: 0 10px !important;
    overflow-x: auto !important;
    &::-webkit-scrollbar {
      z-index: 11;
      width: 6px;
    }
    &::-webkit-scrollbar:horizontal {
      height: 6px;
    }
    &::-webkit-scrollbar-thumb {
      border-radius: 5px;
      width: 6px;
      background: #666;
    }
    &::-webkit-scrollbar-corner,
    &::-webkit-scrollbar-track {
      background: #1e1e1e;
    }
    &::-webkit-scrollbar-track-piece {
      background: #1e1e1e;
      width: 6px;
    }
  }
  .line-numbers-rows {
    position: absolute;
    pointer-events: none;
    top: 12px;
    bottom: 12px;
    left: 0;
    font-size: 100%;
    width: 40px;
    text-align: center;
    letter-spacing: -1px;
    border-right: 1px solid rgba(0, 0, 0, 0.66);
    user-select: none;
    counter-reset: linenumber;
    span {
      pointer-events: none;
      display: block;
      counter-increment: linenumber;
      &:before {
        content: counter(linenumber);
        color: #999;
        display: block;
        text-align: center;
      }
    }
  }
  b.name {
    position: absolute;
    top: 7px;
    right: 45px;
    z-index: 1;
    color: #999;
    pointer-events: none;
  }
  .copy-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 1;
    color: #ccc;
    background-color: #525252;
    border-radius: 6px;
    display: none;
    font-size: 14px;
    width: 32px;
    height: 24px;
    outline: none;
  }
}
</style>
